import { ref } from 'vue';
import { useStore } from 'vuex';
import { useRoute } from 'vue-router';
import {ElMessageBox} from 'element-plus'
export function useAttackDetail() {
  const store = useStore();
  const route = useRoute();

  type BasicInfoType = {
    tag: { tag: any; modifiable: boolean }[];
  };
  type Block = {
    img?: any;
    tip?: any;
    tag?: any;
    list?: any;
  };
  const category = ref('');
  const detialInfo = ref<any>({});
  const basicInfo = ref<BasicInfoType>({ tag: [] });
  const changeList = ref([]);
  const id = ref(route.query.id || '');
  const blockList = ref<Block[]>([]);
  const AssetChanged = ref({
    category,
    dataId: id.value,
    pageNumber: 1,
    pageSize: 100,
    noPage: false,
  });

  //获取页面的基础信息
  const getDetialById = async (path: string) => {
    const { code, data } = await store.dispatch('detail/getAssetVulById', id.value);
    detialInfo.value = code === 0 ? data : {};
    basicInfo.value = code === 0 ? data : {};
  };
  //获取左侧信息
  const getLeftBaseInfo = async () => {
    const { code, data } = await store.dispatch('attack/getBaseInfo', {
        businessType: category.value,
        id: id.value,
    });
    basicInfo.value = code === 0 ? data : {};
    detialInfo.value = code === 0 ? data : {};
  };
  //获取资产变更信息
  const getAssetChanged = async () => {
    AssetChanged.value.category = category.value;
    AssetChanged.value.dataId = id.value;
    const { code, data } = await store.dispatch(
      'detail/getAssetChanged',
      AssetChanged.value,
    );
    if (code === 0) {
      changeList.value = changeList.value.concat(data.list);

      if (data.list.length < 10) {
        AssetChanged.value.noPage = true;
      }
    }
  };
//左侧标签操作：删除标签
  const delTag = async (tag: any) => {
    const data = {
      exposedCode: category.value,
      assetId: id.value,
      tag,
    };
    const { code } = await store.dispatch('api/deleteTags', data);
    if (code === 0) {
      basicInfo.value.tag = basicInfo.value.tag.filter(
        (item: any) => item.tag !== tag,
      );
    }
  };
//左侧标签操作：添加标签
const addTag=async(value)=> {
  const data = {
    assets: [
      {
        category: category.value,
        assetId: id.value,
      },
    ],
    tags: [value],
  };

  const { code } = await store.dispatch('api/appendTagsV2', data);

  if (code === 0) {
    basicInfo.value.tag.push({
      tag: value,
      modifiable: true,
    });
  }
}
//获取分析评估结果
  const getAnalysis = async () => {
    const params = {
    
        businessType: category.value,
        id: id.value,
      
    };

    const [response1, response2, response3] = await Promise.all([
      store.dispatch('attack/getAnalysisExposureRisk', params),
      store.dispatch('attack/getAnalysisExposureLevel', params),
      store.dispatch('attack/getAnalysisExposureVulnerability', params),
    ]);

    const exposureStatus = store.state.enum.exposureStatus;
    const attackStatus = store.state.enum.attackStatus;

    const updateBlockList = (index: any, levelData: any, statusList: any, imgName: string) => {
      if (levelData.code === 0) {
        const level = statusList.find(
          (el: any) => el.type === levelData.data.level,
        );

        if (level) {
          blockList.value[index] = {
            ...blockList.value[index],
            img: require(`~/static/images/${imgName}${level.tag}.png`),
            tip: level.value || '',
            tag: level.tag,
            list:
              levelData.data.items.map((item) => {
                return { ...item, icon: item.existBln };
              }) || [],
          };
        }
      }
    };

    updateBlockList(0, response1, exposureStatus, 'risk-');
    updateBlockList(1, response2, exposureStatus, 'level-');
    updateBlockList(2, response3, attackStatus, 'Attack-');
  };

  const showPrompt = (item: any) => {
    ElMessageBox.confirm('确认此资产为误报吗？')
    .then(async () => {
      const { code, message = '标记成功！' } = await store.dispatch(
        'expose/markAsset',
        {
          assetType: category.value,
          assetId: id.value,
          misinformation: 1,
        },
      );

      if (code === 0) {
        // this.$sdk.toast.success(message)
      }
    })
    .catch((_) => {
    });
  };

  return {
    category,
    detialInfo,
    basicInfo,
    changeList,
    AssetChanged,
    blockList,
    getDetialById,
    getLeftBaseInfo,
    getAssetChanged,
    delTag,
    addTag,
    getAnalysis,
    showPrompt,
  };
}







